<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt" %>


<%@ page import="sg.edu.nus.iss.cats.model.User,sg.edu.nus.iss.cats.controller.*,
         sg.edu.nus.iss.cats.dataaccess.DAOException"%>
         
<%                   
        User modifiedUser = new User();
        
        
        String userId = request.getParameter("userId");
        String modifiedName = request.getParameter("name");
        String modifiedPassword = request.getParameter("password");
        String modifiedEmployeeId =  request.getParameter("employeeId");
        
        String[] modifiedRoleIds =  request.getParameterValues("roleIds");
        
        if (modifiedName == null || modifiedPassword == null || 
            modifiedEmployeeId == null || modifiedName.equals("") || modifiedPassword.equals("") || modifiedEmployeeId.equals("") || 
            modifiedRoleIds == null || modifiedRoleIds.equals("")){
            
            HTMLUtil.displayException(request,response,"Sorry, the user details could not be updated. \n Please check the name, password, employeedId and roles.....");
            return;
            
        }
        
        modifiedUser.setUserId(userId);
        modifiedUser.setName(modifiedName);
        modifiedUser.setPassword(modifiedPassword);
        modifiedUser.setEmployeeId(modifiedEmployeeId);
        
        modifiedUser.setRoleIds(modifiedRoleIds);
        try{
            UserManager userManager = new UserManager();
            userManager.updateUser (modifiedUser);
            
        }catch(DAOException e){
            HTMLUtil.displayException(request,response,"Sorry, the user details could not be updated. \n Please check the name, password, employeedId and roles.....");
                    return;
                    
        }
       //Forward to page which lists users
       RequestDispatcher rd = getServletContext().getRequestDispatcher( 
               "/admin/view");
               
        if (rd != null){
           rd.forward(request,response);
        }
  
%>